package com.sunzm.spark.core;

import org.apache.spark.SparkConf;
import org.apache.spark.SparkContext;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.*;
import org.apache.spark.rdd.RDD;

import java.util.Arrays;
import java.util.Iterator;

public class JavaWordCount {
    public static void main(String[] args) {
       /* SparkConf conf = new SparkConf().setAppName("RDD java wordcount示例")
                .setMaster("local[*]");

        JavaSparkContext jsc = new JavaSparkContext(conf);

        JavaRDD<String> dataRDD = jsc.textFile("data/spark/rdd/word.txt");

        JavaRDD<String> wordRDD = dataRDD.flatMap(new FlatMapFunction<String, String>() {

            @Override
            public Iterator<String> call(String line) throws Exception {
                String[] wordArray = line.split(",");

                return Arrays.stream(wordArray).iterator();
            }
        });*/

      /*  JavaRDD<Tuple2<String, Integer>> wordAndOne = wordRDD.map(new Function<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> call(String word) throws Exception {
                return Tuple2.apply(word, 1);
            }
        });*/

     /*   JavaPairRDD<String, Integer> wordAndOne = wordRDD.mapToPair(new PairFunction<String, String, Integer>() {

            @Override
            public Tuple2<String, Integer> call(String s) throws Exception {
                return null;
            }
        });

        JavaPairRDD<String, Integer> result = wordAndOne.reduceByKey(new Function2<Integer, Integer, Integer>() {
            @Override
            public Integer call(Integer c1, Integer c2) throws Exception {
                return c1 + c2;
            }
        });

        result.foreach(new VoidFunction<Tuple2<String, Integer>>() {
            @Override
            public void call(Tuple2<String, Integer> t) throws Exception {
                String word = t._1;
                Integer count = t._2;
                System.out.println("单词:" + word + ", 次数：" + count);
            }
        });*/

      //  jsc.stop();
    }
}
